# -*- coding: utf-8 -*-
# @Time    : 2020/12/11 1:25
# @Author  : Guo_Zhanyu
# @File    : GomokuClientBlack.py
from GomokuPyQt5 import *
from PyQt5.QtCore import QTimer
import requests
import datetime
from aiGzy2 import *


class BoardOnline(BoardUI):
    def __init__(self, lines, who_am_i):
        super(BoardOnline, self).__init__(lines)
        self.who_am_i = who_am_i
        # 获取的数据
        self.board_dict_get = dict()
        # 定时从服务器更新棋盘
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.operate)
        self.timer.start(100)
        self.mode = 0
        self.uiDependence.pushButton_2.clicked.connect(self.setPlayerMode)
        self.uiDependence.pushButton_3.clicked.connect(self.setAIMode)

        self.ai = AI(lines, who_am_i)
        pass

    def setPlayerMode(self):
        self.mode = 0
        pass

    def setAIMode(self):
        self.mode = 1
        pass

    def closeEvent(self, event):
        self.initBoard()
        self.postBoardDictOnline(-1, -1)
        pass

    def operate(self):
        self.getBoardDictOnline()
        self.setBoard(self.board_dict_get['board'])
        self.ai.setBoard(self.board_dict_get['board'])
        runner = self.board_dict_get['runner']
        win = self.board_dict_get['win']
        new_last = Point(self.board_dict_get['lastX'], self.board_dict_get['lastY'])
        print(new_last.x)
        if new_last.x != -1 and new_last.y != -1:
            self.last = new_last
            pass

        if runner == 'black':
            self.cur_runner = BLACK_CHESSMAN
        elif runner == 'white':
            self.cur_runner = WHITE_CHESSMAN
            pass

        if win == 1:
            self.winner = BLACK_CHESSMAN
        elif win == 2:
            self.winner = WHITE_CHESSMAN
        else:
            self.winner = None
            pass

        self.update()
        if self.mode == 1 and self.cur_runner.name == self.who_am_i.name and self.winner is None:
            AI_point = self.ai.AI_drop()
            self.drop(self.who_am_i, AI_point)
            self.cur_runner = self.getNextRunner(self.cur_runner)
            self.last = AI_point
            self.postBoardDictOnline(AI_point.x, AI_point.y)
            self.update()
            pass

        pass

    def getBoardDictOnline(self):
        # '/board' GET 得到board_dict_get
        r1 = requests.get(self.url + 'board')
        self.board_dict_get = eval(r1.text)
        # print(board_dict_get)
        pass

    def postBoardDictOnline(self, x, y):
        board_dict_post = dict()
        board_dict_post['x'] = x
        board_dict_post['y'] = y
        # print(board_dict_post)
        # '/board' POST 提交board_dict
        r3 = requests.post(self.url + 'board', data=board_dict_post)
        print(str(datetime.datetime.now().strftime('%H:%M:%S')) + r3.text)
        pass

    def drop(self, chessman, point):
        self._board[point.y][point.x] = chessman.value
        pass

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.winner is None and self.cur_runner.name == self.who_am_i.name and self.mode == 0:
                mouse_pos = [event.pos().x(), event.pos().y()]
                click_point = self.getClickPoint(mouse_pos)
                if click_point is not None:
                    if self.canDrop(click_point):
                        self.drop(self.cur_runner, click_point)
                        self.cur_runner = self.getNextRunner(self.cur_runner)
                        self.last = click_point
                        self.postBoardDictOnline(click_point.x, click_point.y)
                        pass
                    pass
                else:
                    print('超过棋盘范围...')
                    pass
                self.update()
                pass
            pass
        elif event.button() == Qt.RightButton:
            if self.winner is not None:
                self.initBoard()
                self.postBoardDictOnline(-1, -1)
                self.update()
                pass
            pass
        pass

    pass


def main():
    who_am_i = BLACK_CHESSMAN
    app = QApplication(sys.argv)
    board_ui = BoardOnline(LINES, who_am_i)
    sys.exit(app.exec_())
    pass


if __name__ == '__main__':
    main()
    pass
